<?php 
set_time_limit(0);
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
$config= require './data/conf/db.php';
require_once './c/Classes/PHPExcel.php';

$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '准考证')
->setCellValue('B1', '区县')
->setCellValue('C1', '学校')
->setCellValue('D1', '年级')
->setCellValue('E1', '姓名')
->setCellValue('F1', '指导老师')
->setCellValue('G1', '考场')
->setCellValue('H1', '身份证')
->setCellValue('I1', '手机号')
;

 
$mysqli = new mysqli($config['DB_HOST'], $config['DB_USER'], $config['DB_PWD'], $config['DB_NAME']);
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}
$sql="SELECT * FROM `app_student` order by id ";
$query =$mysqli->query($sql);
$index=2;
while ($list=$query->fetch_array()){
    $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A'.$index, $list['hao'])
    ->setCellValue('B'.$index, $list['qu'])
    ->setCellValue('C'.$index, $list['school'])
    ->setCellValue('D'.$index, $list['nianji'])
    ->setCellValue('E'.$index, $list['name'])
    ->setCellValue('F'.$index, '')
    ->setCellValue('G'.$index, $list['kc'])
    ->setCellValue('H'.$index, ' '.$list['idcard'])
    ->setCellValue('I'.$index, $list['phone'])
    ;
    $index++;
}

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('学生信息');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="学生信息.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
//$objWriter->save();
exit;